import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '@/views/Login'
// import Layout from '@/views/Layout'
// import Home from '@/views/Home'
// import User from '@/views/User'
// import Search from '@/views/Search'
// import SearchResult from '@/views/Search/SearchResult'
// import ArticleDetail from '@/views/ArticleDetail'
// import UserEditor from '@/views/User/UserEdit'
// import Chat from '@/views/Chat'

// 路由懒加载
// () => import(/* webpackChunkName: "Login" */ '@/views/Login')

import { getToken } from '@/utils/token'
Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/login'
}, {
  path: '/login',
  component: () => import(/* webpackChunkName: "Login" */ '@/views/Login'),
  beforeEnter: (to, from, next) => {
    // 路由独享守卫
    if (getToken()?.length > 0) {
      // next(false) // 留在原地
      next('/layout/home')
    } else {
      next() // 其他情况统统放行
    }
  }
}, {
  path: '/layout',
  component: () => import(/* webpackChunkName: "Layout" */ '@/views/Layout'),
  children: [{
    path: 'home',
    component: () => import(/* webpackChunkName: "Home" */ '@/views/Home'),
    meta: { scrollT: 0 } // 保存首页离开时首页的位置
  }, {
    path: 'user',
    component: () => import(/* webpackChunkName: "User" */ '@/views/User')
  }]
}, {
  path: '/search',
  component: () => import(/* webpackChunkName: "Search" */ '@/views/Search')
}, { // 搜索结果页
  path: '/search_result/:kw',
  component: () => import(/* webpackChunkName: "SearchResult" */ '@/views/Search/SearchResult')
}, { // 文章详情页
  path: '/detail',
  component: () => import(/* webpackChunkName: "ArticleDetail" */ '@/views/ArticleDetail')
},
{
  path: '/user_editor',
  component: () => import(/* webpackChunkName: "UserEditor" */ '@/views/User/UserEdit')
},
{
  // 聊天页面
  path: '/chat',
  component: () => import(/* webpackChunkName: "Chat" */ '@/views/Chat')
}]

const router = new VueRouter({
  routes
})

// 路由 - 全局前置守卫
// router.beforeEach((to, from, next) => {
//   if (getToken()?.length > 0 && to.path === '/login') {
//     next(false) // 留在原地
//     next('/layout/home')
//   } else {
//     next() // 其他情况统统放行
//   }
// })

export default router
